package com.xuecheng.manage_cms.service.impl;

import com.xuecheng.framework.domain.cms.CmsSite;
import com.xuecheng.framework.domain.cms.request.QuerySiteRequest;
import com.xuecheng.framework.domain.cms.response.CmsSiteCode;
import com.xuecheng.framework.domain.cms.response.SiteResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.dao.CmsSiteRepository;
import com.xuecheng.manage_cms.service.CmsSiteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.crypto.Mac;
import java.util.Date;
import java.util.Optional;

@Service
public class CmsSiteServiceImpl implements CmsSiteService {
    @Autowired
    CmsSiteRepository  cmsSiteRepository;
    @Override
    public QueryResponseResult findList(int page, int size, QuerySiteRequest siteRequest) {
        //设置参数
        if(null == siteRequest){
            siteRequest = new QuerySiteRequest();
        }
        CmsSite cmsSite = new CmsSite();
        if(StringUtils.isNotEmpty(siteRequest.getSiteName())){
            cmsSite.setSiteName(siteRequest.getSiteName());
        }
        if(StringUtils.isNotEmpty(siteRequest.getSitePort())){
            cmsSite.setSitePort(siteRequest.getSitePort());
        }
        if(StringUtils.isNotEmpty(siteRequest.getSiteWebPath())){
            cmsSite.setSiteWebPath(siteRequest.getSiteWebPath());
        }
        //设置查询对象
        ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("siteName", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<CmsSite> example = Example.of(cmsSite, matcher);
        //设置分页参数
        if(page <= 0){
            page = 1;
        }
        page -= 1;
        if(size <= 0){
            size = 10;
        }
        Pageable pageable = PageRequest.of(page, size);
        Page<CmsSite> pagedata = cmsSiteRepository.findAll(example, pageable);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(pagedata.getContent());
        queryResult.setTotal(pagedata.getTotalElements());
        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
        return queryResponseResult;
    }

    @Override
    public SiteResult add(CmsSite cmsSite) {
        //查询是否存在此主机,端口,访问路径的记录
        CmsSite dbcmssite = cmsSiteRepository.findBySiteDomainAndSitePortAndSiteWebPath(cmsSite.getSiteDomain(), cmsSite.getSitePort(), cmsSite.getSiteWebPath());
        if(null != dbcmssite){
            ExceptionCast.cast(CmsSiteCode.CMS_ADDSITE_EXISTS);
        }
        //设置id为空
        cmsSite.setSiteId(null);
        cmsSiteRepository.save(cmsSite);
        return new SiteResult(CommonCode.SUCCESS,cmsSite);
    }

    @Override
    public CmsSite findById(String id) {
        Optional<CmsSite> optional = cmsSiteRepository.findById(id);
        if(optional.isPresent()){
            CmsSite cmsSite = optional.get();
            return cmsSite;
        }
        return null;
    }

    @Override
    public SiteResult edit(String id, CmsSite cmsSite) {
        //查询是否存在此记录
        CmsSite one = this.findById(id);
        if(null == one){
            ExceptionCast.cast(CmsSiteCode.CMS_EDITSITE_NOTEXISTS);
        }
        one.setSiteDomain(cmsSite.getSiteDomain());
        one.setSiteName(cmsSite.getSiteName());
        one.setSiteWebPath(cmsSite.getSiteWebPath());
        one.setSitePort(cmsSite.getSitePort());
        cmsSiteRepository.save(one);
        return new SiteResult(CommonCode.SUCCESS,cmsSite);
    }

    @Override
    public ResponseResult delete(String id) {
        cmsSiteRepository.deleteById(id);
        return new QueryResponseResult(CommonCode.SUCCESS,null);
    }
}
